package br.uniriotec.pssgbd.trab.model.bo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import br.uniriotec.pssgbd.trab.model.entidade.FaixaTemperatura;
import br.uniriotec.pssgbd.trab.model.entidade.Marca;

public class MarcaBO {

	protected PadraoBO dao = new PadraoBO();
	Connection connection;
	
	public MarcaBO(){
		try{
			
			connection = dao.criaConexao(false);
			
		}
		catch (Exception e){
			JOptionPane.showMessageDialog(null, ""+e.getMessage());
		}
	}
	
	public List<Marca> listar(){
		String sql = "select * from marca";
		List<Marca> marcas= new ArrayList<Marca>();
		
		try {
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while (rs.next()) {
				Marca marca = new Marca();
				marca.setMarca(rs.getString("nomefantasia"));
				int id = rs.getInt("marcaid");
				marca.setCodMarca(id);
				marca.setCnpj(rs.getString("cnpj"));
				marca.setRazaoSocial(rs.getString("razaosocial"));
				marcas.add(marca);
			}
		} catch (SQLException e) {
			Exception erro = new Exception(e.getMessage());
		}
		
		return marcas;
	}
	

	public Marca recuperaMarca(Integer codMarca) {
		String sql = "select * from marca where marcaid=?";
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Marca marca= null;
		
		try {
			stmt = connection.prepareStatement(sql);
			stmt.setInt(1, codMarca);
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				marca = new Marca();
				marca.setMarca(rs.getString("nomefantasia"));
				marca.setCodMarca(Integer.parseInt(rs.getString("marcaid")));
				marca.setCnpj(rs.getString("cnpj"));
				marca.setRazaoSocial(rs.getString("razaosocial"));
			}
		} catch (SQLException e) {
			Exception erro = new Exception("Erro ao recuperar a marca!");
		}
		
		return marca;
		
	}
	
//	public static void main (String [] args){
//		MarcaBO u = new MarcaBO();
//		Marca rec = u.recuperaMarca(1);
//		System.out.println("MARCA RECUP:"+rec.getMarca());
//	}

}
